﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace GameTools.Models
{
    [Serializable]
    public class Config
    {
        public const string CONFIG_FILE_PATH = "config.ini";

        public string PlayerName { get; set; }

        public string MapName { get; set; }

        public string ScriptName { get; set; }

        //验证时间
        public int AlarmInterval { get; set; }

        //验证窗口时离线
        public bool IsVerificationOffline { get; set; }

        //报警频率
        public int AlarmPitch { get; set; }


        //台阶等待时间
        public int StepWaitTime { get; set; }

        //按键比率,默认90
        public int KeyRate { get; set; }
        //移动距离,默认5
        public int MoveDistance { get; set; }
        //拾取时间，默认50
        public int PickCycleTime { get; set; }
        /// <summary>
        /// 生命百分比
        /// </summary>
        public int LifeRate { get; set; }
        /// <summary>
        /// 魔力百分比
        /// </summary>
        public int MagicRate { get; set; }

        /// <summary>
        /// 生命药水剂量
        /// </summary>
        public int LifePotion { get; set; }
        /// <summary>
        /// 魔力药水剂量
        /// </summary>
        public int MagicPotion { get; set; }

        /// <summary>
        /// 生命循环时间
        /// </summary>
        public int LifeCycleTime { get; set; }
        /// <summary>
        /// 魔力循环时间
        /// </summary>
        public int MagicCycleTime { get; set; }




        //调试模式
        public bool IsDebug { get; set; }

        public static Config GetDefault()
        {
            return new Config()
            {
                AlarmInterval = 20000,
                IsVerificationOffline = false,
                AlarmPitch = 440,
                StepWaitTime = 1500,
                KeyRate = 90,
                MoveDistance = 5,
                PickCycleTime = 50,
                LifeRate = 80,
                MagicRate = 50,
                LifePotion = 5,
                MagicPotion = 1,
                ScriptName = "默认脚本",
                MapName = "默认地图",
                PlayerName = "默认人物",
                LifeCycleTime = 1500,
                MagicCycleTime = 2000,
            };
        }


        public static Config Load()
        {
            Config instance = null;
            if (File.Exists(CONFIG_FILE_PATH))
            {
                try
                {
                    using (FileStream stream = File.Open(CONFIG_FILE_PATH, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        BinaryFormatter format = new BinaryFormatter();
                        instance = (Config)format.Deserialize(stream);
                    }
                }
                catch
                {
                    instance = Config.GetDefault();
                }

            }
            else
            {
                instance = Config.GetDefault();
            }

            return instance;
        }

        public void Save()
        {
            using (FileStream stream = File.Open(CONFIG_FILE_PATH, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                BinaryFormatter format = new BinaryFormatter();
                format.Serialize(stream, this);
            }

        }


    }



}
